/*
 * Copyright 2010 The WicketForge-Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wicketforge.facet;

import java.util.ArrayList;
import java.util.List;

import org.jdom.Element;

import wicketforge.converter.PathConverter;
import wicketforge.converter.PathConverterFactory;
import wicketforge.library.WicketLibraryType;
import com.intellij.facet.FacetConfiguration;
import com.intellij.facet.impl.ui.FacetEditorsFactoryImpl;
import com.intellij.facet.ui.FacetEditorContext;
import com.intellij.facet.ui.FacetEditorTab;
import com.intellij.facet.ui.FacetValidatorsManager;
import com.intellij.facet.ui.libraries.FrameworkLibraryValidator;
import com.intellij.framework.library.DownloadableLibraryService;

/**
 * WicketForgeFacetConfiguration
 */
public class WicketForgeFacetConfiguration implements FacetConfiguration {

	private static final String RESOURCEURLS_TAG = "resourceUrls";
	private static final String RESOURCEURL_TAG = "resourceUrl";
	private static final String URL = "url";

	private static final String PATHCONVERTERS_TAG = "pathConverters";
	private static final String PATHCONVERTER_TAG = "pathConverter";

	List<String> resourceUrls = new ArrayList<String>();
	List<PathConverter> pathConverters = new ArrayList<PathConverter>();

	public FacetEditorTab[] createEditorTabs(FacetEditorContext editorContext,
			FacetValidatorsManager validatorsManager) {
		FrameworkLibraryValidator validator = FacetEditorsFactoryImpl.getInstanceImpl().createLibraryValidator(
				DownloadableLibraryService.getInstance().createDescriptionForType(WicketLibraryType.class),
				editorContext,
				validatorsManager,
				"wicket"
		);
		validatorsManager.registerValidator(validator);
		return new FacetEditorTab[] {new WicketFacetEditorTab(editorContext)};
	}

	@Deprecated
	public void readExternal(Element element) {
		readResourceUrls(element);
		readPathConverters(element);
	}

	private void readResourceUrls(final Element element) {
		Element resourceUrlsElement = element.getChild(RESOURCEURLS_TAG);
		if (resourceUrlsElement != null) {
			List resourceUrlsChildren = resourceUrlsElement.getChildren(RESOURCEURL_TAG);
			if (resourceUrlsChildren != null) {
				for (Object child : resourceUrlsChildren) {
					if (child instanceof Element) {
						resourceUrls.add(((Element) child).getAttributeValue(URL));
					}
				}
			}
		}
	}

	private void readPathConverters(final Element element) {
		Element pathConvertersElement = element.getChild(PATHCONVERTERS_TAG);
		if (pathConvertersElement != null) {
			List pathConvertersElementChildren = pathConvertersElement.getChildren(PATHCONVERTER_TAG);
			if (pathConvertersElementChildren != null) {
				for (Object child : pathConvertersElementChildren) {
					if (child instanceof Element) {
						pathConverters.add(PathConverterFactory.readPathConverter((Element) child));
					}
				}
			}
		}
	}

	@Deprecated
	public void writeExternal(Element element) {
		writeResourceUrls(element);
		writePathConverters(element);
	}

	private void writePathConverters(final Element element) {
		Element pathConvertersElement = new Element(PATHCONVERTERS_TAG);
		element.addContent(pathConvertersElement);
		for (PathConverter converter : pathConverters) {
			Element child = new Element(PATHCONVERTER_TAG);
			PathConverterFactory.writePathConverter(converter, child);
			pathConvertersElement.addContent(child);
		}
	}

	private void writeResourceUrls(final Element element) {
		Element resourceUrlsElement = new Element(RESOURCEURLS_TAG);
		element.addContent(resourceUrlsElement);
		for (String resourceUrl : resourceUrls) {
			Element child = new Element(RESOURCEURL_TAG);
			child.setAttribute(URL, resourceUrl);
			resourceUrlsElement.addContent(child);
		}
	}
}
